package sg.edu.nus.iss.club;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.Serializable;


public class Booking implements Serializable{
	private Date startDate; 
	private Date endDate;
	private Member member;
	private Facility facility;
	public static final String DATE_FORMAT = "MMdd-HH:mm";
	private static final SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT);
	
	public Booking (Member m, Facility f, Date startDate, Date endDate) throws BadBookingException {
		this.startDate = startDate;
		this.endDate = endDate;
		this.member = m;
		this.facility = f;
		
		if (member == null) throw (new BadBookingException ("Member is null"));
		if (facility == null)  throw (new BadBookingException ("Facility is null"));
		if (startDate == null)  throw (new BadBookingException ("Start Date is null"));
		if (endDate == null) throw (new BadBookingException ("End Date is null"));
		if (startDate.after(endDate))  throw (new BadBookingException ("Start Date is after End Date"));
		
	}
	
	
	public Date getStartDate() {
		return startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public Member getMember() {
		return member;
	}
	public Facility getFacility() {
		return facility;
	}
	
	public Boolean overlaps(Booking b2) {
		if (b2!= null) {
			if(b2.getFacility().equals(getFacility())){
				if (startDate.before(b2.getStartDate())&& endDate.after(b2.getStartDate())){
					return true;
				}else if (startDate.before(b2.getEndDate())&& endDate.after(b2.getEndDate())){
					return true;
				}else {
					return false;
				}
				
				
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
	
	public String toString() {
		return member.toString() +"@"+ facility.toString() +" From "+ sdf.format(startDate) + " TO "  + sdf.format(endDate);
	}
	
	public void show() {
		System.out.println(this);
	}
	
	public Boolean equals(Booking b2){
		if (startDate.equals(b2.getStartDate()) &&
				endDate.equals(b2.getEndDate()) &&
				facility.equals(b2.getFacility()) &&
				member.equals(b2.getMember())){
			return true;
		}else {
			return false;
		}
		
	}
}
